package com.orange.adminapi.controller.merchantReport;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.orange.core.service.merchantReport.MerchantReportService;
import com.orange.core.pojo.BR;
import com.orange.core.pojo.PageReqDto;
import com.orange.core.pojo.R;
import com.orange.core.pojo.entity.merchantReport.dto.MerchantReportDTO;
import com.orange.core.pojo.entity.merchantReport.vo.MerchantReportVO;
import com.orange.core.pojo.req.merchantReport.MerchantReportPageReq;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>ClassName:MerchantReportController</p>
 * <p>Description: 商户报表controller</p>
 * <p>Author: lwm</p>
 * <p>Date: 2025/5/30 0:10</p>
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/merchantReport")
public class MerchantReportController {

    private final MerchantReportService merchantReportService;

    /**
     * 商户报表分页列表
     * @param pageReqDto
     * @return
     */
    @PreAuthorize("hasAuthority(T(com.orange.core.pojo.enums.EPermission).MERCHANT_REPORT_LIST.permission.identifier)")
    @PostMapping("/page")
    public R<Page<MerchantReportVO>> page(@RequestBody PageReqDto<MerchantReportPageReq> pageReqDto) {
        Page<MerchantReportVO> page = merchantReportService.page(pageReqDto);
        return R.ok(page);
    }


    /**
     * 商户报表详情
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority(T(com.orange.core.pojo.enums.EPermission).MERCHANT_REPORT_INFO.permission.identifier)")
    @GetMapping("/info/{id}")
    public R<MerchantReportVO> info(@PathVariable("id") int id){
        return R.ok(merchantReportService.info(id));
    }

    /**
     * 商户报表新增
     * @param merchantReportDTO
     * @return
     */
    @PreAuthorize("hasAuthority(T(com.orange.core.pojo.enums.EPermission).MERCHANT_REPORT_SAVE.permission.identifier)")
    @PostMapping
    public R<Boolean> save(@RequestBody @Validated MerchantReportDTO merchantReportDTO) {
        return BR.result(merchantReportService.save(merchantReportDTO));
    }

    /**
     * 商户报表修改
     * @param merchantReportDTO
     * @return
     */
    @PreAuthorize("hasAuthority(T(com.orange.core.pojo.enums.EPermission).MERCHANT_REPORT_UPDATE.permission.identifier)")
    @PutMapping
    public R<Boolean> update(@RequestBody @Validated MerchantReportDTO merchantReportDTO) {
        return BR.result(merchantReportService.update(merchantReportDTO));
    }


    /**
     * 商户报表批量删除
     * @param ids
     * @return
     */
    @PreAuthorize("hasAuthority(T(com.orange.core.pojo.enums.EPermission).MERCHANT_REPORT_DELETE.permission.identifier)")
    @DeleteMapping("/{ids}")
    public R<Boolean> remove(@PathVariable("ids") Integer[] ids){
        return BR.result(merchantReportService.deleteMerchantReportByIds(ids));
    }

    /**
     * 商户报表下载
     * @param id
     * @return
     */
    @GetMapping("/download/{id}")
    public R<String> downloadFile(@PathVariable("id") Integer id){
        return R.ok(merchantReportService.downloadFile(id));
    }
}
